package com.xintonghua.base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xintonghua.base.R;
import com.xintonghua.base.utils.MyUtils;


/**
 * 圆角弹出框口
 */
public class ConfirmDialog extends Dialog implements View.OnClickListener {
    private OnRightListener mOnRightListener;
    private OnLeftListener mOnLeftListener;
    private TextView tv_msg, tv_title, tv_left, tv_right;
    public static ConfirmDialog dialog;


    public static void showDialog(Activity mActivity, String title, String msg, String leftText, String rightText, OnLeftListener leftListener, OnRightListener rightListener) {
        showDialog(mActivity, title, msg, leftText, rightText, leftListener, rightListener, Gravity.CENTER);
    }


    public static void showDialog(Activity mActivity, String title, String msg, String leftText, String rightText, OnLeftListener leftListener, OnRightListener rightListener, int gravity) {
        showDialog(mActivity, title, msg, leftText, rightText, leftListener, rightListener, gravity, true);
    }

    public static void showDialog(Activity mActivity, String title, String msg, String leftText, String rightText, OnLeftListener leftListener, OnRightListener rightListener, int gravity, boolean cancelable) {
        if (MyUtils.activityIsFinished(mActivity)) {
            return;//activity被销毁，不显示
        }
        dialog = new ConfirmDialog(mActivity, false);
        //不能点外面取消
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setContext(title, msg, leftText, rightText, gravity);
        dialog.setRightListener(rightListener);
        dialog.setLeftListener(leftListener);
        dialog.setOwnerActivity(mActivity);
        dialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface d) {
                dialog = null;
            }
        });
        dialog.show();
    }


    public static void showAlert(Activity mActivity, String title, String msg, String rightText, OnRightListener rightListener) {
        dialog = new ConfirmDialog(mActivity, true);

        //不能点外面取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        dialog.setContext(title, msg, null, rightText, Gravity.LEFT);
        if (rightListener != null) {
            dialog.setRightListener(rightListener);
        }

        dialog.setOwnerActivity(mActivity);

        dialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface d) {
                dialog = null;
            }
        });

        dialog.show();
    }

    public static void dismissDialog(Activity mActivity) {
        if (dialog != null && dialog.isShowing() && dialog.getOwnerActivity() != null && dialog.getOwnerActivity().toString().equals(mActivity.toString())) {
            dialog.dismiss();
            dialog = null;
        }
    }


    public ConfirmDialog(Context context, boolean alert) {
        super(context, R.style.utils_circle_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_confim, null);

        tv_title = (TextView) view.findViewById(R.id.tv_title);//标题
        tv_msg = (TextView) view.findViewById(R.id.tv_msg);//内容
        tv_left = (TextView) view.findViewById(R.id.tv_left);
        tv_right = (TextView) view.findViewById(R.id.tv_right);

        tv_right.setBackgroundResource(alert ? R.drawable.shape_alert_bottom : R.drawable.shape_confirm_right);

        //绑定两个按钮的事件
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);


        setContentView(view);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;

        Display d = window.getWindowManager().getDefaultDisplay();
        params.width = (int) (d.getWidth() * 0.7);

        window.setAttributes(params);
    }

    public ConfirmDialog(Context context, int resId, float width) {
        super(context, R.style.utils_circle_dialog);
        View view = LayoutInflater.from(context).inflate(resId, null);

        tv_title = (TextView) view.findViewById(R.id.tv_title);//标题
        tv_msg = (TextView) view.findViewById(R.id.tv_msg);//内容
        tv_left = (TextView) view.findViewById(R.id.tv_left);
        tv_right = (TextView) view.findViewById(R.id.tv_right);

        //绑定两个按钮的事件
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);


        setContentView(view);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;

        Display d = window.getWindowManager().getDefaultDisplay();
        params.width = (int) (d.getWidth() * width);

        window.setAttributes(params);
    }


    public void setContext(String title, String context, String leftText, String rightText, int gravity) {
        tv_msg.setText(context);
        tv_msg.setGravity(gravity);
        if (leftText == null) {
            tv_left.setVisibility(View.GONE);
        } else {
            tv_left.setVisibility(View.VISIBLE);
            tv_left.setText(leftText);
        }
        tv_right.setText(rightText);
        tv_title.setText(title);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() == R.id.tv_right && mOnRightListener != null) {//确认
            mOnRightListener.onClick(this);
        } else if (v.getId() == R.id.tv_left && mOnLeftListener != null) {//取消
            mOnLeftListener.onClick(this);
        }
    }

    public void setRightListener(OnRightListener l) {
        mOnRightListener = l;
    }

    public void setLeftListener(OnLeftListener l) {
        mOnLeftListener = l;
    }

    public interface OnRightListener {
        void onClick(ConfirmDialog dialog);
    }

    public interface OnLeftListener {
        void onClick(ConfirmDialog dialog);
    }
}
